/**
 * 功能:TK_INVENTORY 控制器类
 * 开发人员:chengd
 * 创建时间:2012/4/23 17:06:07
 */
package com.towker.frame.controller.warehouse;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.sun.java.swing.plaf.windows.resources.windows;
import com.towker.web.MyRequest;
import com.towker.core.page.PageModel;
import com.towker.core.page.PageNav;
import com.towker.core.util.CollectionUtil;
import com.towker.core.util.RefGoodsType;
import com.towker.core.util.StringUtil;
import com.towker.frame.controller.BaseController;
import com.towker.frame.model.basicData.Goods;
import com.towker.frame.model.sell.RefGoods;
import com.towker.frame.model.warehouse.Inventory;
import com.towker.frame.model.warehouse.SendBills;
import com.towker.frame.model.warehouse.WarehouseAccounts;
import com.towker.frame.service.basicData.DepotsService;
import com.towker.frame.service.basicData.GoodsService;
import com.towker.frame.service.sell.RefGoodsService;
import com.towker.frame.service.warehouse.InventoryService;
import com.towker.frame.service.warehouse.WarehouseAccountsService;
import com.towker.frame.share.sys.user.UserShare;

//TK_INVENTORY
@Controller
@SuppressWarnings("unchecked")
@RequestMapping("/manage/warehouse")
public class InventoryController extends BaseController
{
	private InventoryService service;
	private DepotsService depotsService;//仓库信息
	private GoodsService goodsService;//物品信息
	private WarehouseAccountsService warehouseAccountsService;

	//业务对象注入
	public void setInventoryService(InventoryService service)
	{
		this.service = service;
	}
	public void setDepotsService(DepotsService depotsService)
	{
		this.depotsService = depotsService;
	}
	
	public void setGoodsService(GoodsService goodsService)
	{
		this.goodsService = goodsService;
	}
	
	public void setWarehouseAccountsService(WarehouseAccountsService warehouseAccountsService)
	{
		this.warehouseAccountsService = warehouseAccountsService;
	}

	//添加
	@RequestMapping("/addInventory{viewName}")
	public ModelAndView addInventory(HttpServletResponse response,HttpServletRequest request, Inventory po, @PathVariable String viewName) throws IOException
	{
		ModelAndView result = null;
		if ("1".equals(viewName))
		{
			result = new ModelAndView();
			Map map = new HashMap();
			List deportList = depotsService.findByCoCode(UserShare.getCurrentUserCoCode(request));//仓库信息
			result.addObject("deportList", deportList);
			result.addObject("inventory_code", service.getNewInventoryCode());
			result.setViewName("/manage/warehouse/addInventory.jsp");
		}
		else if ("2".equals(viewName))
		{
			PrintWriter writer = response.getWriter();
			try
			{
				po.setCreater_code(UserShare.getCurrentUserCode(request));//获取当前用户代码
				po.setCreater_id(UserShare.getCurrentUserId(request).intValue());//获取当前用户id
				po.setAuthCoCode(UserShare.getCurrentUserCoCode(request));
				//po.setAuthDepCode(UserShare.getCurrentUserDepCode(request));
				
				service.add(po);
				MyRequest myRequest = new MyRequest(request);
				String goodsInfo = myRequest.getString("goodsInfo");
				processRefGoods(request, RefGoodsType.INVENTORY_GOODS, po.getId(), goodsInfo);
				
				writer.write("添加盘点表成功！");
			}
			catch (Exception e)
			{
				e.printStackTrace();
				writer.write("添加盘点表失败！" + e.getCause());
			}
		}
		return result;
	}

	//删除
	@RequestMapping("/delInventory")
	public void delInventory(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		MyRequest req = new MyRequest(request);
		service.delByIds(CollectionUtil.toLongArray(req.getLongArray("keyIndex", 0)));
		response.sendRedirect(req.getRefererURL());
	}

	//修改
	@RequestMapping("/updInventory{viewName}")
	public ModelAndView updInventory(HttpServletRequest request, HttpServletResponse response, Inventory po, @PathVariable String viewName) throws IOException
	{
		ModelAndView result = null;
		MyRequest req = new MyRequest(request);
		if ("1".equals(viewName))
		{
			Long id = req.getLong("keyIndex");
			po = service.getById(id);
			result = new ModelAndView();
			result.addObject("po", po);
			Map map = new HashMap();
			List deportList = depotsService.findByCoCode(UserShare.getCurrentUserCoCode(request));//仓库信息
			result.addObject("deportList", deportList);
			result.addObject("refGoods", refGoodsService.getByInfoType(RefGoodsType.INVENTORY_GOODS.getValue(),new Integer(String.valueOf(id)) ));
			result.setViewName("/manage/warehouse/updInventory.jsp");
		}
		else if ("2".equals(viewName))
		{
			PrintWriter writer = response.getWriter();
			String goodsInfo = req.getString("goodsInfo");
			try
			{
				service.update(po);
				refGoodsService.deleteByInfoType(RefGoodsType.INVENTORY_GOODS.getValue(), po.getId());
				processRefGoods(request, RefGoodsType.INVENTORY_GOODS, po.getId(), goodsInfo);
				writer.write("修改盘点表成功！");
			}
			catch (Exception e)
			{
				e.printStackTrace();
				writer.write("修改盘点表失败！" + e.getCause());
			}
		}
		return result;
	}

	//获得分页
	@RequestMapping("/getInventory")
	public ModelAndView getInventory(HttpServletRequest request,HttpServletResponse response, @RequestParam(value="page", defaultValue="1")int page)
	{
		int pageSize = 10;
		MyRequest req = new MyRequest(request);
		Map map = req.getParameterValueMap(false, true);
		map.put("authCoCode", UserShare.getCurrentUserCoCode(request));
		//map.put("authDepCode", UserShare.getCurrentUserDepCode(request));
		PageModel<Inventory> pageModel = service.getPage(page, pageSize, map);
		ModelAndView result = new ModelAndView();
		PageNav pageNav = new PageNav(request, pageModel);
		result.addObject("pageModel", pageModel);
		result.addObject("pageNav", pageNav);
		result.setViewName("/manage/warehouse/getInventory.jsp");
		return result;
	}

	//明细
	@RequestMapping("/getInventoryById")
	public ModelAndView getInventoryById(HttpServletRequest request, HttpServletResponse response)
	{
		Inventory po = null;
		MyRequest req = new MyRequest(request);
		Long id = req.getLong("keyIndex");
		po = service.getById(id);
		ModelAndView result = new ModelAndView();
		result.addObject("po", po);
		result.addObject("depot", depotsService.getByCode(po.getDepot_code()));
		result.addObject("refGoods", refGoodsService.getByInfoType(RefGoodsType.INVENTORY_GOODS.getValue(),new Integer(String.valueOf(id)) ));
		result.setViewName("/manage/warehouse/getInventoryById.jsp");
		return result;
	}
	
	
	
	/*//记账
	@RequestMapping("/accInventory{viewName}")
	public ModelAndView accInventory(HttpServletRequest request, HttpServletResponse response,@PathVariable String viewName)throws IOException
	{
		ModelAndView result = null;
		Inventory po = null;
		MyRequest req = new MyRequest(request);
		Long id = req.getLong("keyIndex");
		po = service.getById(id);
		if ("1".equals(viewName))
		{
			result = new ModelAndView();
			result.addObject("acc","acc");
			result.addObject("po", po);
			result.addObject("refGoods", refGoodsService.getByInfoType(RefGoodsType.INVENTORY_GOODS.getValue(),new Integer(String.valueOf(id)) ));
			result.setViewName("/manage/warehouse/getInventoryById.jsp");
		}else if ("2".equals(viewName)) {
			PrintWriter writer = response.getWriter();
			try
			{
				//添加数据到仓库帐中
				po.setAcc_status(1);
				service.update(po);
				
				List<RefGoods> refgoodList = refGoodsService.getByInfoType(RefGoodsType.INVENTORY_GOODS.getValue(), po.getId());
				if (refgoodList!=null && refgoodList.size()!=0) {
					for (int i = 0; i < refgoodList.size(); i++) {
						RefGoods refGoods = (RefGoods)refgoodList.get(i);
						Goods goods = goodsService.getById(new Long(refGoods.getGoods_id()));
						*//**
						 * 记录到仓库帐中
						 *//*
						SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
						WarehouseAccounts warehouseAccounts = new WarehouseAccounts();
						warehouseAccounts.setAcc_period(po.getAcc_period());//财务统计核算期
						warehouseAccounts.setInfo_type("盘点表");//仓库帐类型
						warehouseAccounts.setGoods_sn(goods.getGoods_sn());//物料代码
						warehouseAccounts.setDep_code(po.getDepot_code());//仓库代码
						warehouseAccounts.setCreate_date(po.getInventory_date());//日期
						warehouseAccounts.setCreate_time(po.getInventory_time());//时间
						warehouseAccounts.setSys_date(format.format(new Date()));//生成仓库帐时间
						warehouseAccounts.setStock_num(goods.getStock());//当前库存数量
						warehouseAccounts.setAmount(refGoods.getAmount());//物品数量
						//warehouseAccounts.setSend_amount(0);//已下采购单要求送货数量, 根据采购单号 查询采购数量
						String remark = "单号："+po.getInventory_code();
						warehouseAccounts.setRemark(remark);//凭证号（单号）、部门、客户等>
						warehouseAccountsService.add(warehouseAccounts);
					}
				}
				
				
				writer.write("记账成功！");
			}
			catch (Exception e)
			{
				e.printStackTrace();
				writer.write("记账失败！" + e.getCause());
			}
		}
		return result;
	}*/
}
